package com.roger.myHome.controller;

import com.roger.myHome.aspect.WebLog;
import com.roger.myHome.entity.dto.BloodSugarAddReq;
import com.roger.myHome.entity.qto.BloodSugarReq;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.BloodSugarService;
import com.roger.myHome.util.DateUtil;
import com.roger.myHome.util.SystemUtil;
import com.roger.myHome.util.UserHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bloodSugar")
@Slf4j
public class BloodSugarController {

    private static final Map<String, Float> HP = new HashMap<String, Float>();

    static {
        HP.put("空腹",6.1F);
        HP.put("早餐后2小时",7.8F);
        HP.put("中餐前", 6.1F);
        HP.put("中餐后1.5小时",7.8F);
        HP.put("中餐后2小时",7.8F);
        HP.put("中餐后2.5小时",7.8F);
        HP.put("中餐后3小时",7.8F);
        HP.put("中餐后3.5小时", 7.8F);
        HP.put("晚餐前", 6.1F);
        HP.put("晚餐后2小时", 7.8F);
        HP.put("晚餐后3小时", 7.8F);
        HP.put("随机", 7.8F);
    }

    @Resource
    private BloodSugarService bloodSugarService;

    @Resource
    private UserHelper userHelper;


    @PostMapping("/page")
    @WebLog()
    public EUDataGridResult page(@RequestParam(value="page",required=false) String page,
                                            @RequestParam(value="rows",required=false) String rows, HttpServletRequest req){

        BloodSugarReq bloodSugarReq = new BloodSugarReq();

        if (req.getParameter("measureTime1") != null && !"".equals(req.getParameter("measureTime1"))) {
            bloodSugarReq.setMeasureTime1(req.getParameter("measureTime1") + " 00:00");
        }
        if (req.getParameter("measureTime2") != null && !"".equals(req.getParameter("measureTime2"))) {
            bloodSugarReq.setMeasureTime2(req.getParameter("measureTime2") + " 23:59");
        }

        bloodSugarReq.setTimePeriod(req.getParameter("timePeriod"));
        bloodSugarReq.setLevel(req.getParameter("level"));

        bloodSugarReq.setPage((Integer.parseInt(page) - 1) * Integer.parseInt(rows));
        bloodSugarReq.setRows(Integer.parseInt(rows));
        log.info("[{}]:BloodSugarController page",userHelper.getLoginUser(req).getLoginName());
        return bloodSugarService.page(bloodSugarReq);
    }



    @PostMapping("/add")
    @ResponseBody
    public String add(BloodSugarAddReq req){
        req.setId(SystemUtil.getId());
        Object[] obj = getLevel(req.getBloodSugarValue(), req.getTimePeriod());
        req.setBloodSugarLevel(obj[1].toString());
        req.setBloodSugarException(Float.valueOf(obj[0].toString()));
        req.setMeasureTime(req.getMeasureTime().substring(0,16));
        req.setWeek(DateUtil.dateToWeek(req.getMeasureTime().substring(0,10)));
        return bloodSugarService.add2(req);
    }

    @GetMapping("/export")
    public void Export(HttpServletResponse response) {
        bloodSugarService.export(response);
    }

    private Object[] getLevel(Float value, String timePeriod){
        Float standValue = HP.get(timePeriod);
        Float exception = value - standValue;
        Object[] obj = new Object[2];
        obj[0] = exception;
        if (value < 4.0) {
            obj[1] = "偏低";
        } else if (value > standValue) {
            obj[1] = "偏高";
        } else {
            obj[0] = 0;
            obj[1] = "正常";
        }
        //log.info("obj===[{}],[{}]",obj[0],obj[1]);
        return obj;
    }

    @DeleteMapping("/delete/{id}")
    public String delete (@PathVariable Long id){
        log.info("删除id[{}]",id);
        return bloodSugarService.delete(id);
    }

}
